var SystemStatic = {};
SystemStatic.contextpath = "";
SystemStatic.initPage = function(contextpath) {
	SystemStatic.contextpath = contextpath;

	// 类型按钮
	$(document).on("click", ".typeId", function() {
		var typeId=$(this).attr("typeId");
		SystemStatic.initTable(typeId);
	});
	
	// 添加大类
	$(document).on("click", "#add", function() {
		SystemStatic.addTypeId();
	});
	
	// 添加子类
	$(document).on("click", "#addChild", function() {
		SystemStatic.systemStaticQuery();
	});
}

SystemStatic.systemStaticQuery = function() {
	layui.use(['table','form'], function() {
	$.ajax({
		type : "POST",
		url : "/web2019/system/systemStaticQuery.action",
		dataType : "json",
		async: false,
		success : function(data) {
			if (data.code == "0000") {
				SystemStatic.addDataId(data.list);
			} 
		},
		error : function(data) {

		}
	});
	});
}

SystemStatic.addTypeId= function(){
	layui.use(['table','form'], function() {
    layer.open({
        type: 1
        ,title: '添加归类'
        ,area: ['590px', '460px']
        ,shade: 0
        ,maxmin: true
        ,content: SystemStatic.getAddTypeIdHtml()
        ,btn: ['添加', '取消'] //只是为了演示
        ,yes: function(){
        	SystemStatic.ajaxAddTypeId();
        }
        ,btn2: function(){
          layer.closeAll();
        }
        ,success: function(layero){
        	layui.use('form', function() {
        		var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        		form.render();
        		});
        }
      });
	});
}
/**
 * 添加归类
 */
SystemStatic.ajaxAddTypeId =function(){
	var dataId =$("#dataId").val();
	var dataName =$("#dataName").val();
	var code =$("input[name='code']").val() ;
	$.ajax({
		type : "POST",
		url : "/web2019/system/ajaxAddTypeId.action",
		data : {"dataId":dataId,"dataName":dataName,"code":code},
		dataType : "json",
		beforeSend:function(){
			layer.load(1);
		},
		complete:function(){
			layer.close(layer.index-1);
		},
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.close(index);
					window.location.href="/"+SystemStatic.contextpath+"/system/systemParamsQuery.action";
				});
			} 
		},
		error : function(data) {

		}
	});
}

/**
 * 添加归类
 */
SystemStatic.ajaxAddDataId =function(){
	var dataId =$("#dataId").val();
	var dataName =$("#dataName").val();
	var code =$("#code").val();
	var typeId =$("#typeId").val();
	$.ajax({
		type : "POST",
		url : "/web2019/system/ajaxAddDataId.action",
		data : {"dataId":dataId,"dataName":dataName,"code":code,"typeId":typeId},
		dataType : "json",
		beforeSend:function(){
			layer.load(1);
		},
		complete:function(){
			layer.close(layer.index-1);
		},
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.close(index);
					window.location.href="/"+SystemStatic.contextpath+"/system/systemParamsQuery.action";
				});
			} 
		},
		error : function(data) {

		}
	});
}


SystemStatic.addDataId =function(list){
	layui.use(['table','form'], function() {
	    layer.open({
	        type: 1
	        ,title: '添加值'
	        ,area: ['590px', '460px']
	        ,shade: 0
	        ,maxmin: true
	        ,content: SystemStatic.getAddDataIdHtml(list)
	        ,btn: ['添加', '取消'] //只是为了演示
	        ,yes: function(){
	        	SystemStatic.ajaxAddDataId();
	        }
	        ,btn2: function(){
	          layer.closeAll();
	        }
	        ,success: function(layero){
	        	layui.use('form', function() {
	        		var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
	        		form.render();
	        		});
	        }
	      });
		});	
}

SystemStatic.getAddDataIdHtml= function(list){
	var html="";
	html+='<form class="layui-form">';
	html+='<fieldset class="layui-elem-field layui-field-title"';
	html+='	style="margin-top: 20px;">';
	html+='<legend style="font-weight:200px;font-size:16px;">添加值</legend>';
	html+='</fieldset>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">归属</label>';
	html+='<div class="layui-input-inline">';
	html+='  <select id="typeId" name="typeId">';
	for(var i=0;i<list.length;i++){
		html+='  <option value="'+list[i].dataId+'">'+list[i].dataName+' </option>';
	}
	html+='  </select>';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">值</label>';
	html+='<div class="layui-input-inline">';
	html+='	<input type="text" name="dataId" id="dataId" style="width:300px;"';
	html+='	autocomplete="off" placeholder="值" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">名称</label>';
	html+='<div class="layui-input-inline">';
	html+='	<input type="text" name="dataName" id="dataName"  style="width:300px;"';
	html+='	autocomplete="off" placeholder="请输入名称" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">编码名称</label>';
	html+='<div class="layui-input-inline">';
	html+='	<input type="text" name="code" id="code" style="width:300px;"';
	html+='	autocomplete="off" placeholder="生成编码名称" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='</form>';
	return html;
}

SystemStatic.getAddTypeIdHtml= function(obj){
	var html="";
	html+='<form class="layui-form">';
	html+='<fieldset class="layui-elem-field layui-field-title"';
	html+='	style="margin-top: 20px;">';
	html+='<legend style="font-weight:200px;font-size:16px;">添加归类</legend>';
	html+='</fieldset>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">编码</label>';
	html+='<div class="layui-input-inline">';
	html+='	<input type="text" name="dataId" id="dataId"  style="width:300px;"';
	html+='	autocomplete="off" placeholder="请输入编码" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">名称</label>';
	html+='<div class="layui-input-inline">';
	html+='	<input type="text" name="dataName" id="dataName"  style="width:300px;"';
	html+='	autocomplete="off" placeholder="请输入名称" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='  <div class="layui-form-item" pane="">';
	html+='    <label class="layui-form-label">生成编码</label>';
	html+='    <div class="layui-input-inline">';
	html+='     <input type="radio" name="code" value="1" title="是">';
	html+='    <input type="radio" name="code" value="0" title="否">';
	html+='   </div>';
	html+='  </div>';
	html+='</form>';
	return html;
}



SystemStatic.initTable =function(typeId){
	layui.use(['table','form'], function() {
		var table = layui.table;
		var form = layui.form;
		// 第一个实例
		table.render({
			elem : '#static_list_table',
			url : '/web2019/system/ajaxQueryStaticList.action' // 数据接口
			,
			page : false // 开启分页
			, 
			where: 
			{
				typeId :typeId
            },
			response : {
				statusName : 'code' // 规定数据状态的字段名称，默认：code
				,
				statusCode : "0000" // 规定成功的状态码，默认：0
				,
				msgName : 'message' // 规定状态信息的字段名称，默认：msg
				,
				countName : 'total' // 规定数据总数的字段名称，默认：count
			},
			cols : [ [ // 表头
			{
				field : 'id',
				title : '编号',
				width : 100,
				sort : true,
			}, {
				field : 'typeId',
				title : '类型',
				width : 100
			}, {
				field : 'dataId',
				title : '值',
				width : 120,
				sort : true
			}, {
				field : 'dataName',
				title : '名称',
				width : 160,
				sort : true
			}, {
				field : 'code',
				title : '编码',
				width : 120,
				sort : true
			}, {
				title : '操作',
				templet : SystemStatic.setOperate,
				width : 160,
				align : 'center',
				fixed : 'right'
			} ] ]
		});
		  //监听工具条
		  table.on('tool(static_list_table_filter)', function(obj){
		    var data = obj.data;
		    if(obj.event === 'del'){
		      layer.confirm('真的删除行么', function(index){
		    	  SystemStatic.ajaxDeleteStatic(data);
		      });
		    } 
		  });
		
	});
}

SystemStatic.setOperate=function(data){
	var btnHtml="";
	btnHtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>';
	return btnHtml;
}

SystemStatic.ajaxDeleteStatic= function(data1) {
	$.ajax({
		type : "POST",
		url : "/web2019/system/ajaxDeleteSystemStatic.action",
		dataType : "json",
		data : data1.field,
		beforeSend:function(){
			layer.load(1);
		},
		complete:function(){
			layer.close(layer.index-1);
		},
		data : data1,
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.close(index);
					window.location.href="/"+SystemStatic.contextpath+"/system/systemParamsQuery.action";
				});
			} else {
				
			}
		},
		error : function(data) {

		}
	});
}